package com.lifeservice.console.controller;

import com.lifeservice.common.annotation.Log;
import com.lifeservice.common.core.controller.BaseController;
import com.lifeservice.common.core.domain.AjaxResult;
import com.lifeservice.common.core.page.TableDataInfo;
import com.lifeservice.common.enums.BusinessType;
import com.lifeservice.common.utils.poi.ExcelUtil;
import com.lifeservice.console.domain.LifeSchedule;
import com.lifeservice.console.service.ILifeScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 排班Controller
 *
 * @author lifeservice
 * @date 2023-02-27
 */
@RestController
@RequestMapping("/console/schedule")
public class LifeScheduleController extends BaseController {
    @Autowired
    private ILifeScheduleService lifeScheduleService;

    /**
     * 查询排班列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LifeSchedule lifeSchedule) {
        startPage();
        List<LifeSchedule> list = lifeScheduleService.selectLifeScheduleList(lifeSchedule);
        return getDataTable(list);
    }

    /**
     * 导出排班列表
     */
    @Log(title = "排班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LifeSchedule lifeSchedule) {
        List<LifeSchedule> list = lifeScheduleService.selectLifeScheduleList(lifeSchedule);
        ExcelUtil<LifeSchedule> util = new ExcelUtil<LifeSchedule>(LifeSchedule.class);
        util.exportExcel(response, list, "排班数据");
    }

    /**
     * 获取排班详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(lifeScheduleService.selectLifeScheduleById(id));
    }

    /**
     * 新增排班
     */
    @Log(title = "排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LifeSchedule lifeSchedule) {
        return toAjax(lifeScheduleService.insertLifeSchedule(lifeSchedule));
    }

    /**
     * 修改排班
     */
    @Log(title = "排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LifeSchedule lifeSchedule) {
        return toAjax(lifeScheduleService.updateLifeSchedule(lifeSchedule));
    }

    /**
     * 删除排班
     */
    @Log(title = "排班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lifeScheduleService.deleteLifeScheduleByIds(ids));
    }

    @Log(title = "删除排班", businessType = BusinessType.DELETE)
    @PostMapping("/deleteById")
    public AjaxResult deleteById(@RequestBody LifeSchedule lifeSchedule) {
        return toAjax(lifeScheduleService.deleteLifeScheduleById(lifeSchedule.getId()));
    }
}
